import java.io.*;
import java.util.Scanner;

/**
 * @Author YuanYuan
 * @Date 2022/10/8
 * @Time 12:39
 */
public class Demo2 {
    /**
     * 进行普通文件的复制
     * @return
    */

    public static void fileCopy() throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件:");
        File srcfile = new File(scanner.next());
        if(!srcfile.isFile()) {
            System.out.println("源文件不存在!");
            return;
        }
        System.out.println("请输入目标文件:");
        File datefile = new File(scanner.next());
        //判断目标文件的上级目录是否存在
        if(!datefile.getParentFile().isDirectory()) {
            System.out.println("目标文件目录不存在!");
            return;
        }
        //字节流读源文件
        InputStream inputStream = new FileInputStream(srcfile);
        //输入目标文件
        OutputStream outputStream = new FileOutputStream(datefile);
        System.out.println("开始复制");
        while(true) {
            //一个字节一个字节读
            int ret = inputStream.read();
            if(-1 == ret) {
                break;
            }
            //讲读的字节写入
            outputStream.write(ret);
        }
        System.out.println("复制完毕");
        inputStream.close();
        outputStream.close();

    }
    public static void main(String[] args) throws IOException {
        fileCopy();
    }

}
